<template>
    <div
        class="order-index"
        v-loading.fullscreen.lock="fullscreenLoading"
        :element-loading-text="info"
        :element-loading-spinner="svg"
        element-loading-svg-view-box="-10, -10, 50, 50"
    >
        <div class="main">
            <el-breadcrumb :separator-icon="ArrowRight">
                <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
                <el-breadcrumb-item to="/cart">购物车</el-breadcrumb-item>
                <el-breadcrumb-item>订单</el-breadcrumb-item>
            </el-breadcrumb>
            <div class="container">
                <!-- 面包屑 -->

                <el-card shadow="hover">
                    <div class="order-current">
                        <el-col :sm="12" :lg="6">
                            <el-result
                                icon="success"
                                :sub-title="subTitle"
                                v-if="orderInfo.is_pay == 1"
                            >
                            </el-result>
                            <el-result
                                icon="success"
                                sub-title="支付成功！"
                                v-else
                            >
                            </el-result>
                        </el-col>
                        <div
                            class="payment"
                            ref="payment"
                            v-if="orderInfo.is_pay == 1"
                        >
                            请在<span class="time">{{ orderTime }}</span
                            >内完成支付。
                        </div>
                        <div class="payment" v-else>
                            <span>您已完成支付！</span>
                        </div>
                        <div class="info">
                            <span>订单编号:{{ orderInfo.order_id }}</span>
                            <span v-if="orderInfo.is_pay == 1"
                                >下单时间:{{ orderInfo.create_time }}</span
                            >
                            <span v-else
                                >支付时间:{{ orderInfo.update_time }}</span
                            >
                            <span
                                >订单金额:<b style="color: red"
                                    >￥{{ orderInfo.total_price }}</b
                                ></span
                            >
                        </div>
                    </div>
                </el-card>
                <el-card shadow="hover">
                    <!-- 收货地址 -->
                    <OrderAddress />
                    <!-- 商品信息 -->
                    <OrderGoodsInfo
                        v-if="orderGoodsINfo"
                        :orderGoodsINfo="orderGoodsINfo"
                    />
                    <!-- 配送方式 -->
                    <OrderDelivery v-if="orderInfo.is_pay == 1" />
                    <!-- 支付方式 -->
                    <OrderPayment v-if="orderInfo.is_pay == 1" />
                    <!-- 金额明细 -->
                    <OrderAmount
                        v-if="orderGoodsINfo"
                        :orderGoodsINfo="orderGoodsINfo"
                        :orderInfo="orderInfo"
                    />
                    <el-button
                        v-if="orderInfo.is_pay == 1"
                        @click="toPayment()"
                        color="rgb(39,186,155)"
                        >立即付款
                    </el-button>
                    <el-button
                        v-else
                        color="rgb(39,186,155)"
                        @click="
                            router.push(
                                `/payment?order_id=${orderInfo.order_id}`
                            )
                        "
                        >已支付
                    </el-button>
                </el-card>
            </div>
        </div>
        <div
            class="to-payment"
            v-loading.fullscreen.lock="fullscreenLoading2"
            element-loading-text="支付信息提交中..."
            :element-loading-spinner="svg"
            element-loading-svg-view-box="-10, -10, 50, 50"
        ></div>
    </div>
</template>

<script setup>
import { useRoute, useRouter } from 'vue-router'
import { ArrowRight } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { findOrderInfoById } from '@/apis/order'
import OrderAddress from './components/order-address.vue'
import OrderAmount from './components/order-amount.vue'
import OrderDelivery from './components/order-delivery.vue'
import OrderGoodsInfo from './components/order-goods-info.vue'
import OrderPayment from './components/order-payment.vue'
import { sendPayment } from '@/apis/payment'
let payment = ref(null)
let info = ref('订单提交中...')
let subTitle = ref('订单信息提交成功！')
let time = ref(1800)
let orderTime = ref('')
let orderInfo = ref('')
let orderGoodsINfo = ref('')
const route = useRoute()
const router = useRouter()
if (route.query.current == 1) {
    info.value = '订单信息查询中...'
    subTitle.value = '订单信息查询成功！'
}
const svg = `
        <path class="path" d="
          M 30 15
          L 28 17
          M 25.61 25.61
          A 15 15, 0, 0, 1, 15 30
          A 15 15, 0, 1, 1, 27.99 7.5
          L 15 15
        " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
      `
const fullscreenLoading = ref(true)
const fullscreenLoading2 = ref(false)
onMounted(() => {
    setTimeout(() => {
        findOrderInfoById(route.query.order_id).then(res => {
            orderInfo.value = res.at(-1)
            res.pop()
            orderGoodsINfo.value = res
            fullscreenLoading.value = false
        })
    }, 1500)
})
setInterval(function () {
    time.value = time.value - 1
    let minute = parseInt(time.value / 60)
    let second = parseInt(time.value % 60)
    orderTime.value = minute + '分' + second + '秒'
    if (time.value == 0) {
        time.value = 1800
    }
}, 1000)
const toPayment = () => {
    fullscreenLoading2.value = true
    sendPayment(orderInfo.value.order_id)
    setTimeout(() => {
        fullscreenLoading2.value = false
        router.push({
            name: 'payment',
            query: {
                order_id: orderInfo.value.order_id,
            },
        })
    }, 1500)
}
</script>

<style lang="scss" scoped>
.order-index {
    --el-color-primary: #da9233;
    width: 100vw;
    min-height: 500px;
    .el-breadcrumb {
        margin-bottom: 10px;
        margin-left: -1080px;
    }
    .main {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .container {
        width: 1240px;

        .el-card {
            &:nth-child(1) {
                height: 150px;
                display: flex;
                flex-direction: column;
                justify-content: center;
                .order-current {
                    display: flex;
                    .payment {
                        font-size: 18px;
                        margin-top: 35px;
                        .time {
                            color: #da9233;
                        }
                    }
                }
                .info {
                    display: flex;
                    flex-direction: column;
                    justify-content: center;
                    margin-left: 50px;
                    font-size: 18px;
                    vertical-align: middle;
                    span {
                        margin-top: 5px;
                        color: #868583;
                    }
                }
            }
            &:nth-child(2) {
                margin-top: 20px;
            }
        }
    }
}
.el-button {
    margin-left: 1000px;
    width: 160px;
    font-size: 18px;
    color: aliceblue;
    height: 50px;
    margin-top: 20px;
}
</style>
